<h2><a href="<?=site_url('company/'.$company->company_name);?>"><?=$company->company_name;?></a> &ndash; <?=$job->job_title;?></h2> 

<div class="block tabs spaces">
	<div class="hd">
		<ul>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/updates');?>">Updates</a></li>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/milestones');?>">Milestones</a></li>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/costs');?>">Costs</a></li>
		<?php if ($permissions->createinvoice):?><li><a>Billing</a></li><?php endif;?>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/setup');?>">Job Setup</a></li>
		</ul>
		<div class="clear"></div>
	</div>

	<div class="bd">

		<h2>Raise an invoice on <?=$job->job_title;?></h2>
		
		<?=form_open('jobs/generateinvoice/'.$job->id, array('class' => 'invoice'));?>

		<?=$this->validation->error_string; ?>

		<input class="hidden" type="hidden" name="companyid" value="<?=$company->id;?>" />
		<input class="hidden" type="hidden" name="jobid" value="<?=$job->id;?>" />

			<div class="column left">
				<fieldset>
					<p>
						<label>Title</label>
						<input class="text" type="text" name="invoice_title" value="<?php  if (strlen($this->validation->invoice_title)) echo $this->validation->invoice_title; else echo $job->job_number.' '.$job->job_title?>" />
					</p>
		
					<p>
						<label>Reference</label>
						<input class="text" type="text" name="invoice_reference" value="<?php if(strlen($this->validation->invoice_reference) ) echo $this->validation->invoice_reference; else echo $job->job_number;?>" />
					</p>
	
					<p>
						<label>Due Date</label>
						<input class="text" type="text" name="invoice_duedate" value="<?php if ($this->validation->invoice_duedate) echo $this->validation->invoice_duedate; else echo date("d/m/Y", mktime(0, 0, 0, date("m"), date("d") + 14, date("Y")));?>" />
					</p>
		
					<p>
						<label>PO Number</label>
						<input class="text" type="text" name="invoice_ponumber" value="<?=$job->ponumber;?>" />
					</p>
				</fieldset>
			</div>
			
<div class="column right">
	<fieldset>
		<p>
			<label>For the attention of</label>
				<?php if ($contacts):?>
				<select class="text" name="attentionof">
					<?php foreach($contacts as $contact):?>
					<option class="text" value="<?=$contact->id;?>" <?php if ($contact->id == $job->contactid) echo ' selected="selected"';?>><?=$contact->firstname.' '.$contact->surname;?></option>
					<?php endforeach;?>
				</select>
				<?php else:?>
				<input type="text" name="attentionof" value="<?=$this->validation->attentionof;?>" />
				<?php endif;?>
		</p>
		<p>
			<label>Address</label>
			<?php if ($addresses):?>
			<select name="address">
				<?php foreach($addresses as $address):?>
				<option value="<?=$address->id;?>"><?=$address->address1.', '.$address->city;?></option>
				<?php endforeach;?>
			</select>
			<?php else:?>
			<textarea class="text" name="address"><?=$this->validation->address;?></textarea>
			<?php endif;?>
		</p>
		<p>
			<label>Description</label>
			<textarea class="text" name="invoice_description"><?=$this->validation->invoice_description;?></textarea>
		</p>
	</fieldset>
</div>
<div class="clear"></div>


<h2>Internal Costs</h2>

<table width="100%" class="invoice">
<thead>
	<tr>
		<thead>
				<td class="include" scope="col">&nbsp;</td>
				<td class="dept">Department / Description</td>
				<td class="allocated">Allocated Hours</td>
				<td class="quote">Quoted Cost</td>
				<td class="time">Time Spent</td>
				<td class="actcost">Actual Cost</td>
				<td class="prevcost">Previously Invoiced</td>
				<td class="cost">Cost</td>
		</thead>
	</tr>
</thead>
<tbody>


<?php if ($costcenters): foreach ($costcenters as $key => $department):?>
<tr>
	<td class="colalt"><input type="checkbox" checked="checked" name="includedepartment[]" value="dep<?=$department->id;?>" />
		<input type="hidden" name="dep<?=$department->id;?>_id" value="<?=$department->id;?>" /></td>
	<td><input type="text" value="<?php if ($departmentlines && isset($departmentlines['dep'.$department->id]) ) echo  $departmentlines['dep'.$department->id]->department; else echo $department->centertitle;?>" name="dep<?=$department->id;?>_department" />
		<input type="text" value="<?php if ($departmentlines && isset($departmentlines['dep'.$department->id]) ) echo  $departmentlines['dep'.$department->id]->description;?>" name="dep<?=$department->id;?>_description" /></td>
	<td class="colalt"><?=$department->allocatedhours;?> (@<?=$department->hourlyrate;?>/hr)</td>
	<td>&pound;<?=($department->allocatedhours * $department->hourlyrate);?></td>
	<td class="colalt"><?=$department->actualhours;?> (@<?=$department->hourlyrate;?>/hr)</td>
	<td>&pound;<?=($department->actualhours * $department->hourlyrate);?></td>
	<td class="colalt">&pound;<?=$department->previouslybilled;?></td>
	<td class="cost">&pound;<input type="text" name="dep<?=$department->id;?>_cost" value="<?php if ($departmentlines && isset($departmentlines['dep'.$department->id]) ) echo  $departmentlines['dep'.$department->id]->cost; else echo max((($department->allocatedhours * $department->hourlyrate) - $department->previouslybilled), 0);?>" /></td>
</tr>
<?php endforeach; endif;?>
</tbody>
</table>

<h2>External Costs</h2>

<table width="100%" class="invoice">
<thead>
		<td class="include" scope="col">&nbsp;</td>
		<td class="dept">Department / Description</td>
		<td class="allocated">Allocated Hours</td>
		<td class="quote">Quoted Cost</td>
		<td class="time">Time Spent</td>
		<td class="actcost">Actual Cost</td>
		<td class="prevcost">Previously Invoiced</td>
		<td class="cost">Cost</td>
</thead>
<tbody>

<?php if ($costs): foreach ($costs as $key => $cost):?>
<tr>
	<td class="colalt"><input type="checkbox" checked="checked" name="includecost[]" value="cos<?=$cost->id;?>" />
		<input type="hidden" name="cos<?=$cost->id;?>_id" value="<?=$cost->id;?>" /></td>
	<td><input type="text" value="<?php if ($costlines && isset($costlines['cos'.$cost->id]) ) echo  $costlines['cos'.$cost->id]->department; else echo $cost->cost_title;?>" name="cos<?=$cost->id;?>_department" />
		<input type="text" value="<?php if ($costlines && isset($costlines['cos'.$cost->id]) ) echo  $costlines['cos'.$cost->id]->description; else echo $cost->cost_description;?>" name="cos<?=$cost->id;?>_description" /></td>
	<td class="colalt"></td>
	<td>&pound;<?=$cost->outgoing_cost;?></td>
	<td class="colalt"></td>
	<td>&pound;<?=$cost->incoming_cost;?></td>
	<td class="colalt">&pound;<?=$cost->previouslybilled;?></td>
	<td class="cost">&pound;<input type="text" value="<?php if ($costlines && isset($costlines['cos'.$cost->id]) ) echo  $costlines['cos'.$cost->id]->cost; else echo max(($cost->outgoing_cost - $cost->previouslybilled), 0);?>" name="cos<?=$cost->id;?>_cost" /></td>
</tr>
<?php endforeach; endif;?>

<?php if ($extraslines):?>
<?php foreach ($extraslines as $key => $line):?>
<tr>
	<td class="colalt"><input type="checkbox" checked="checked" name="includeextras[]" value="<?=$key;?>" /></td>
	<td><input type="text" value="<?=$line->department;?>" name="<?=$key;?>_department" />
		<input type="text" value="<?=$line->description;?>" name="<?=$key;?>_description" /></td>
	<td colspan="5"></td>
	<td class="cost">&pound;<input type="text" value="<?=$line->cost;?>" name="<?=$key;?>_cost" /></td>
</tr>
<?php endforeach;?>
<?php endif;?>
	</tbody>
	
	<tfoot>
		<tr class="addnew">
			<td class="colalt"><button>Add</button></td>
			<td><input type="text" value="" />
				<input type="text" value="" /></td>
			<td class="colalt">&nbsp;</td>
			<td>&nbsp;</td>
			<td class="colalt">&nbsp;</td>
			<td>&nbsp;</td>
			<td class="colalt">&nbsp;</td>
			<td class="cost">&pound;<input type="text" /></td>
		</tr>
	</tfoot>
</table>

<div class="column left">
</div>
<div class="column right">
	<table>
		<thead>
				<td>Totals</td>
				<td>&nbsp;</td>
		</thead>
		<tbody>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td>Unallocated Hours</td>
				<td class="colalt"><?=$unallocatedhours;?></td>
			</tr>
			<tr>
				<td>Unbillable Hours</td>
				<td class="colalt"><?=$unbillablehours;?></td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr class="total">
				<td>Total</td>
				<td class="colalt">&pound;<span class="total"></span></td>
			</tr>
		</tbody>
	</table>
	<p>
		<input class="submit" type="submit" name="submit" value="Save this Invoice" />
		<input class="cancel" type="submit" name="cancel" value="Cancel" /></p>
</div>
<div class="clear"></div>





<?=form_close();?>

</div></div></div></div>

<div id="sidebar" class="yui-b">
	
	<?php if ($otherinvoices):?>
	<div class="block">
		<div class="hd">
			<h3>Current Invoices</h3>
		</div>
		<div class="bd">
			<ul>
			<?php foreach($otherinvoices as $invoice):?>
				<li><a href="<?=site_url('jobs/viewinvoice/'.$invoice->id);?>"><?=$invoice->invoicetitle;?></a> <strong> &pound;<?=$invoice->total_cost;?></strong><br /><?php if ($invoice->paid) echo 'Paid'; else echo 'Outstanding - Due '.date('d/m/Y', strtotime($invoice->duedate));?></li>
			<?php endforeach;?>
			</ul>
		</div>
	<?php endif;?>
